package com.neo.web.controller.pub;

import com.neo.common.annotation.Log;
import com.neo.common.core.controller.BaseController;
import com.neo.common.core.domain.AjaxResult;
import com.neo.common.core.page.TableDataInfo;
import com.neo.common.enums.BusinessType;
import com.neo.common.exception.BusinessException;
import com.neo.common.utils.poi.ExcelUtil;
import com.neo.pub.domain.PhysicalExamPatient;
import com.neo.pub.domain.Schedule;
import com.neo.pub.service.IPhysicalExamPatientService;
import com.neo.pub.service.IScheduleService;
import com.neo.system.domain.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户信息
 * 
 * @author tr
 */
@Controller
@RequestMapping("/pub/physcialExam")
public class PhyscialExamController extends BaseController
{
    private String prefix = "pub/physcialExam";

    @Autowired
    private IPhysicalExamPatientService iPhysicalExamPatientService;


//    @RequiresPermissions("system:user:view")
    @GetMapping()
    public String index()
    {
        return prefix + "/index";
    }

//    @RequiresPermissions("system:user:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PhysicalExamPatient physicalExamPatient)
    {
        String code = physicalExamPatient.getPatientCode();
        String id_cis = physicalExamPatient.getId_cis();
        if(StringUtils.isNotBlank(code)){
            code = code.substring(0,code.length()-2);
            physicalExamPatient.setPatientCode(code);
        }
        if(StringUtils.isNotBlank(physicalExamPatient.getPatientCode())){
           int result = iPhysicalExamPatientService.selectPatientCountByCode(physicalExamPatient);
           if(result == 1){
               physicalExamPatient.setId_cis("1");
               iPhysicalExamPatientService.updatePatientStatus(physicalExamPatient);
               physicalExamPatient.setPatientCode("");
           } else if(result == 0){
               throw new BusinessException("查询无此结果");
           } else if(result > 1){
               throw new BusinessException("查询结果有多条记录，请手工确认");
           }
        }
        startPage();
        List<PhysicalExamPatient> list = iPhysicalExamPatientService.selectPatientList(physicalExamPatient);
        return getDataTable(list);
    }

    @PostMapping("/update")
    @ResponseBody
    public AjaxResult edit(@RequestParam("patientCode")String patientCode, @RequestParam("status")String status)
    {
        String result = "";
        PhysicalExamPatient physicalExamPatient = new PhysicalExamPatient();
        physicalExamPatient.setId_cis(status);
        physicalExamPatient.setPatientCode(patientCode);
        return toAjax(iPhysicalExamPatientService.updatePatientStatus(physicalExamPatient));
    }

    @Log(title = "核酸上报管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PhysicalExamPatient physicalExamPatient)
    {
        List<PhysicalExamPatient> list = iPhysicalExamPatientService.selectPatientList(physicalExamPatient);
        ExcelUtil<PhysicalExamPatient> util = new ExcelUtil<PhysicalExamPatient>(PhysicalExamPatient.class);
        return util.exportExcel(list, "核酸上报数据");
    }

}